<?php
/*
 BMForum Datium! Bulletin Board Systems
 Version : Datium!
 
 This is a freeware, but don't change the copyright information.
 A SourceForge Project.
 Web Site: http://www.bmforum.com
 Copyright (C) Bluview Technology
*/
if (!defined('INBMFORUM')) die("Access Denied");

$thisprog = "setemoticon.php";

if ($useraccess != "1" || $admgroupdata[32] != "1") {
    adminlogin();
} 
if (file_exists("datafile/emoticon.php")) {
    $emotfile = "datafile/emoticon.php";
    $emotfiledata = file($emotfile);
} 

if (!$action) {
    print <<<EOT
  <tr><td bgcolor=#14568A colspan=3><font color=#F9FAFE>
  <strong>$arr_ad_lng[320] $arr_ad_lng[224]</strong>
  </td></tr>
  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center colspan=3>
  <strong>$arr_ad_lng[224]</strong>
  </td></tr>



  <tr>
  <td bgcolor=#F9FCFE valign=middle align=left colspan=3>
    　$arr_ad_lng[537]
  </td>
  </tr>

$table_start
<strong style="color:#FFFFFF;"><a style="color:#FFFFFF;" href="admin.php?bmod=$thisprog&action=add">$arr_ad_lng[538]</a></strong> | <strong><a style="color:#FFFFFF;" href="admin.php?bmod=$thisprog&action=refresh">$arr_ad_lng[1081]</a></strong>
$table_start
 	  <tr bgcolor="#6DA6D1">$arr_ad_lng[539]
</tr>
  <script language="JavaScript" type="text/javascript">
function DelEmot(num){
if(confirm("$arr_ad_lng[540]", "$arr_ad_lng[540]")){
window.location="admin.php?bmod=setemoticon.php&action=del&verify=$admin_log_hash&num="+num;
}
}
</script>
	
EOT;

    $count = count($emotfiledata);
    for ($i = 0;$i < $count;$i++) {
    	$thumbfile = "";
        $emotfd = explode("＊＊", $emotfiledata[$i]);
        if ($emotfd[2] == 1) {
        	$thumbfile = "(<img src='images/face/thumb/$emotfd[1]' border='0' />)";
        }
        print <<<EOT
  <tr>
  <td bgcolor=#F9FAFE valign=middle align=left width=40%>
  <strong><img src=images/face/$emotfd[1] border=0> $thumbfile $emotfd[1]</strong></td>
  <td bgcolor=#F9FAFE valign=middle align=center>
  <input type=text  value="$emotfd[0]"></td>
  <td bgcolor=#F9FAFE valign=middle align=center>
  <a href="admin.php?bmod=$thisprog&action=edit&num=$i">$arr_ad_lng[541]</a> <a href="javascript:DelEmot($i);">$arr_ad_lng[542]</a></td>
  </tr>

EOT;
    } 

    print <<<EOT
  
  <tr bgcolor=E2E8D0>
   <td colspan=3 align=center >
  </tr>

  </td></tr></table></body></html>
EOT;
    exit;
} elseif ($action == "refresh") {
	$newemot = "";
	for ($i = 0;$i < count($emotfiledata); $i++){
		$detail = explode("＊＊", $emotfiledata[$i]);
		if (file_exists("images/face/".$detail[1]) && filetype("images/face/".$detail[1]) != "dir") {
			$emot_list[]=$detail[1];
			$newemot .= $emotfiledata[$i];
		}
	}
    $dh = opendir("images/face");
    while (false !== ($imagefile = readdir($dh))) {
        if (!@in_array($imagefile, $emot_list) && filetype("images/face/".$imagefile) != "dir" && $imagefile != "." && $imagefile != ".." && $imagefile != "") {
        	$thumbfile	= 0;
        	$imagefile	= str_replace("\n", "", $imagefile);
        	$imagefile	= str_replace("\r", "", $imagefile);
        	$emotcode	= explode(".", $imagefile);
        	if (file_exists("images/face/thumb/$imagefile")) $thumbfile = 1;
            $newemot .= ":".$emotcode[0].":" . "＊＊" . $imagefile . "＊＊{$thumbfile}＊＊\n";
        }
    } 
    closedir($dh);
	
	writetofile($emotfile, $newemot);
	
	$new_emotfiledata = file($emotfile);
	
	$newemot = "";
	
	for ($i = 0;$i < count($new_emotfiledata); $i++){
		$detail = explode("＊＊", $new_emotfiledata[$i]);

    	if (file_exists("images/face/thumb/$detail[1]")) $thumbfile = 1;
    	else $thumbfile = 0;
    	
        $newemot .= $detail[0] . "＊＊" . $detail[1] . "＊＊{$thumbfile}＊＊\n";
	}
	
	writetofile($emotfile, $newemot);

print <<<EOT
  <tr><td bgcolor=#14568A colspan=2><font color=#F9FAFE>
  <strong>$arr_ad_lng[320] $arr_ad_lng[224]</strong>
  </td></tr>
  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center colspan=2>
  <strong>$arr_ad_lng[545]</strong>
  </td></tr>

			<tr><td bgcolor=#F9FAFE valign=middle align=left colspan=2>$arr_ad_lng[546]<br /><br />&nbsp;&gt;&gt; <a href="admin.php?bmod=$thisprog">$arr_ad_lng[76]</a></td></tr>
EOT;

} elseif ($action == "add") {
    if ($step != "2") {
        print <<<EOT
  <tr><td bgcolor=#14568A colspan=2><font color=#F9FAFE>
  <strong>$arr_ad_lng[320] $arr_ad_lng[224]</strong>
  </td></tr>
  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center colspan=2>
  <strong>$arr_ad_lng[224]</strong>
  </td></tr>


 	  <tr><form action=admin.php?bmod=$thisprog method="post">
<input type=hidden name="action" value="add"> 
<input type=hidden name="step" value="2"> 
  <td bgcolor=#F9FAFE valign=middle align=center width=40%>
  <strong>$arr_ad_lng[543]</strong></td>
  <td bgcolor=#F9FAFE valign=middle align=center>
  <input type="text" name="emotpic"></td></tr>
 	  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center width=40%>
  <strong>$arr_ad_lng[544]</strong></td>
  <td bgcolor=#F9FAFE valign=middle align=center>
  <input type="text" name="emotcode"></td></tr>
  		  <tr><td bgcolor=#F9FAFE valign=middle align=center colspan=2><input type=submit value="$arr_ad_lng[66]"></td></tr></form>
EOT;
    } else {
    	if (file_exists("images/face/thumb/$emotpic")) $thumbfile = 1;
    	else $thumbfile = 0;
        $newemot = $emotcode . "＊＊" . $emotpic . "＊＊{$thumbfile}＊＊\n";
        writetofile($emotfile, $newemot, "a");

        print <<<EOT
  <tr><td bgcolor=#14568A colspan=2><font color=#F9FAFE>
  <strong>$arr_ad_lng[320] $arr_ad_lng[224]</strong>
  </td></tr>
  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center colspan=2>
  <strong>$arr_ad_lng[545]</strong>
  </td></tr>

			<tr><td bgcolor=#F9FAFE valign=middle align=left colspan=2>$arr_ad_lng[546]<br /><br />&nbsp;&gt;&gt; <a href="admin.php?bmod=$thisprog">$arr_ad_lng[76]</a></td></tr>
EOT;
    } 
} elseif ($action == "del") {
    $count = count($emotfiledata);
    for ($n = 0;$n < $count;$n++) {
        if ($num == $n) $emotfiledata[$n] = "";
    } 
    
    $emotfiledata_wr = implode("", $emotfiledata);
    
    writetofile($emotfile, $emotfiledata_wr);
    


    print <<<EOT
  <tr><td bgcolor=#14568A colspan=2><font color=#F9FAFE>
  <strong>$arr_ad_lng[320] $arr_ad_lng[224]</strong>
  </td></tr>
  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center colspan=2>
  <strong>$arr_ad_lng[547]</strong></td></tr><tr>
 <td bgcolor=#F9FAFE valign=middle align=left colspan=2><br /><br />&nbsp;&gt;&gt; <a href="admin.php?bmod=$thisprog">$arr_ad_lng[76]</a>
  </td></tr>

EOT;
} elseif ($action == "edit") {
    if ($step != "2") {
        $count = count($emotfiledata);
        for ($n = 0;$n < $count;$n++) {
            if ($n == $num) {
                $emotfd = explode("＊＊", $emotfiledata[$n]);
            } 
        } 

        print <<<EOT
  <tr><td bgcolor=#14568A colspan=2><font color=#F9FAFE>
  <strong>$arr_ad_lng[320] $arr_ad_lng[224]</strong>
  </td></tr>
  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center colspan=2>
  <strong>$arr_ad_lng[224]</strong>
  </td></tr>

<form action=admin.php?bmod=$thisprog method="post">
<input type=hidden name="action" value="edit"> 
<input type=hidden name="step" value="2"> 
<input type=hidden name="num" value="$num"> 
 	  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center width=40%>
  <strong>$arr_ad_lng[543]</strong></td>
  <td bgcolor=#F9FAFE valign=middle align=center>
  <input type="text" name="emotpic" value="$emotfd[1]"></td></tr>
 	  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center width=40%>
  <strong>$arr_ad_lng[544]</strong></td>
  <td bgcolor=#F9FAFE valign=middle align=center>
  <input type="text" name="emotcode" value="$emotfd[0]"></td></tr>
  		  <tr><td bgcolor=#F9FAFE valign=middle align=center colspan=2><input type=submit value="$arr_ad_lng[66]"></form></td></tr>
EOT;
    } else {
    	if (file_exists("images/face/thumb/$emotpic")) $thumbfile = 1;
    	
        $newemot = $emotcode . "＊＊" . $emotpic . "＊＊{$thumbfile}＊＊\n";

        $count = count($emotfiledata);
        $fp = fopen($emotfile, "w");
        for ($n = 0;$n < $count;$n++) {
            if ($n != $num) {
                fputs($fp, $emotfiledata[$n]);
            } 
        } 
        fwrite($fp, $newemot);
        fclose($fp);

        print <<<EOT
  <tr><td bgcolor=#14568A colspan=2><font color=#F9FAFE>
  <strong>$arr_ad_lng[320] $arr_ad_lng[224]</strong>
  </td></tr>
  <tr>
  <td bgcolor=#F9FAFE valign=middle align=center colspan=2>
  <strong>$arr_ad_lng[548]</strong>
  </td></tr>

			<tr><td bgcolor=#F9FAFE valign=middle align=left colspan=2>$arr_ad_lng[549]<br /><br />&nbsp;&gt;&gt; <a href="admin.php?bmod=$thisprog">$arr_ad_lng[76]</a></td></tr>
EOT;
    } 
} 

